<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">hello</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">
              {{ item.pubdate | fromNow }}
            </span>
            <van-button size="mini" type="default" @click="replyFn(item)">
              回复 {{ item.reply_count }}
            </van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="likeFn(item)"
        />
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容" v-model="content">
        <van-button
          slot="button"
          size="mini"
          type="info"
          @click="publishCom(item)"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <!-- 回复评论界面 -->

    <van-action-sheet v-model="show" title="标题">
      <reply
        :replyContent="replyContent"
        :artId="artId"
        @changeReplyCount="changeReplyCountFn"
        v-if="show"
      ></reply>
    </van-action-sheet>
  </div>
</template>

<script>
import {
  getCommentList,
  publishComment,
  likeComment,
  cancelLikeComment
} from '@/api/comment'

// 引入回复组件
import reply from './reply.vue'
export default {
  name: 'ArticleComment',
  components: {
    reply
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false,
      offset: null, // 是否加载结束
      content: '',
      show: false,
      replyContent: ''
    }
  },

  methods: {
    // 增加回复数量
    changeReplyCountFn() {
      // 评论数自增1
      this.replyContent.reply_count++
    },
    //   评论回复界面
    replyFn(item) {
      this.show = true
      //   console.log(item)
      this.replyContent = item
    },
    //   获取评论列表
    async onLoad() {
      const res = await getCommentList(this.artId, this.offset)
      //   console.log(res)
      this.list = res.data.results
      this.loading = false
      this.finished = true
    },

    // 发布评论
    async publishCom() {
      const res = await publishComment(this.artId, this.content)
      console.log(res)
      this.list.unshift(res.data.new_obj)
      //   清空输入框
      this.content = ''
    },

    // 评论点赞
    async likeFn(item) {
      // 判断当前的is_liking 确认点赞还是取消点赞
      if (item.is_liking) {
        await cancelLikeComment(item.com_id)
        this.$toast('取消点赞')
      } else {
        await likeComment(item.com_id)
        this.$toast('点赞成功')
      }
      item.is_liking = !item.is_liking
    }
  },

  computed: {
    artId() {
      return this.$route.params.id
    }
  }
}
</script>

<style scoped lang="less">
.van-cell {
  //   background-color: rgb(207, 241, 239) !important;
  border: 1px solid rgb(189, 228, 231);
}

// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
